<template>
    <div class="tags">
        <el-tag v-for="(item, index) in tags" :key="item.path" :closable="item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'" @click="changeMenu(item)"
            @close="handleClose(item, index)" size="small">
            {{ item.label }}
        </el-tag>
    </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
    name: 'CommonTag',
    data() {
        return {

        };
    },
    methods: {
        ...mapMutations('tab',['closeTags']),

        //  点击tags跳转的功能
        changeMenu(val) {
            this.$router.push({ name: val.name });
        },
        //  点击tags删除的功能
        handleClose(val, index) {
            //  调用store的 mutations
            this.closeTags(val);
            const length = this.tags.length;

            //  删除之后跳转逻辑
            if (val.name !== this.$route.name) {
                return
            }
            //  表示删除的是最后一页
            if (index === length) {
                this.$router.push({
                    name: this.tags[index - 1].name
                })
            } else {
                this.$router.push({
                    name: this.tags[index].name
                })
            }
        }
    },
    computed: {
        ...mapState({ tags: state => state.tab.tabsList })
    },
    mounted() {

    },
}
</script>

<style lang="less" scoped>
.tags {
    padding: 5px;
    // margin: 5px;
    .el-tag {
        margin-top: -10px;
        margin-right: 5px;
        cursor: pointer;
    }
}
</style>